<?php

/**
 * @author Edison P. Quinones Jr
 * @version  1.0.0
 * @copyright 2008
 */

class JumpsitesController extends Zend_Controller_Action
{
	function init()
	{
		$this->initView();
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$this->view->webInfo = $webInfoNamespace->webInfo;
		//Zend_Loader::loadClass('HeaderLink');
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		Zend_Loader::loadClass('JumpsiteDisplay');
		Zend_Loader::loadClass('Page');
		Zend_Loader::loadClass('Layout');
		Zend_Loader::loadClass('LayoutPartPartGroupWidget');
		Zend_loader::loadClass('Wallpaper');
		Zend_loader::loadClass('GroupjumperAll');

		$countWords = strlen($_SERVER['REQUEST_URI']);
		if (strpos($_SERVER['REQUEST_URI'], '/', $countWords-1) == true)
		{
			$this->_redirect(substr_replace($_SERVER['REQUEST_URI'], '', $countWords-1));
			//$this->view->testingLang = substr_replace($_SERVER['REQUEST_URI'], '', $countWords-1);
		}
		$this->view->user = Zend_Auth::getInstance()->getIdentity();

		# Links
		$registry = Zend_Registry::getInstance();
		$this->view->links = $registry->get('linksArray');
		# end of links
	}
	function indexAction()
	{
		//Header
		$this->view->keywords = 'Edison, Quinones, Jr';
		$this->view->description = 'This is the philippines international airport';
		$this->view->author = 'Edison P. Quinones Jr.';
		$this->view->robots = 'Index';
		$this->view->title = 'Groupjump. The World on the Web!';

		//Page Located
		$this->view->stringToCompareWithTheLink = 'Jumpsites';
		$this->view->PageContentTitle = 'leftLabel';
	}
	function widgetinfoAction()
	{

	}
	function articlesAction()
	{

	}
	function viewAction()
	{
		error_reporting(0);
		$webInfoNamespace  = new Zend_Session_Namespace('webInfo');
		$webInfo = $webInfoNamespace->webInfo;
		
		$filter = new Zend_Filter_StripTags();
		$jumpsiteId = trim($filter->filter($this->_request->getParam('id')));
		$this->view->id = $jumpsiteId;
		$goto = trim($filter->filter($this->_request->getParam('goto')));
		
		if ($jumpsiteId == null)
		{
			$this->_redirect($webInfo.'Jumpsites');
		}

		$this->view->jumpsiteId = $jumpsiteId;

		#------------ Footer Links-------------------
		$footerLinks = '';
		$registry = Zend_Registry::getInstance();
		$footerLinksArray = $registry->get('linksArray');

		foreach ($footerLinksArray as $key => $val)
		{
			$footerLinks = $footerLinks.'<li><a href="'.$webInfo.$val['linksUrl'].'">'.ucwords($val['linkName']).'</a></li>';
		}

		$groupjumperAll = new GroupjumperAll();
		$userLogged = Zend_Auth::getInstance()->getIdentity();
		if(isset($userLogged))
		{
			$where = 'group_id = '.$jumpsiteId.' AND jumper_id = '.$userLogged->id;
			try {
				$isMember = $groupjumperAll->fetchRow($where);
			}
			catch (Exception $e){
				
			}
			if ($isMember->id == null)
			{
				$this->view->isNotMember = true;
			}
			$footerLinks = $footerLinks.'<li><a href="'.$webInfo.'Jumpstart">My Jumpstart</a></li>';
		}
		#---------- end of footer links-------------------


		$jumpsiteDisplay = new JumpsiteDisplay();
		$this->view->jumpsite = new ArrayIterator($jumpsiteDisplay->getGroupToDisplay($jumpsiteId));

		if ($this->view->jumpsite == null)
		{
			$this->_redirect($webInfo);
		}

		#----- get the link tab links and description of the page ------
		$page = new Page();
		$where = 'group_id = '.$jumpsiteId;
		$this->view->tabLinks = $page->fetchAll($where, 'id');
		$addFooterLinks = $page->fetchAll($where, 'id');
		
		if($goto == null)
		{
			$goto = array();
			$count = 0;
			foreach ($addFooterLinks as $key)
			{
				$goto[$count] = $key->id;
				$count++;
			}
			$goto = $goto[0];
			$this->view->tabIsHome = $goto;
			
		}
		$where = 'group_id = '.$jumpsiteId.' and id = '.$goto;
		$tabLinks = $page->fetchRow($where);
		//syntax: fetchAll($where, $order, $limit, $offset);
		$this->view->description = $tabLinks->desc ;
		#--- end of getting the tab links -----

		#--- get the layout of the page
		$layout = new Layout();
		$where = 'id = '.$tabLinks->layout_id;
		$jsLayout = $layout->fetchRow($where, 'id');
		
		$this->view->jsLayout = $jsLayout;
		#--- end of getting the layout of the page

		#-------- Widget display -------
		$layoutPartPartGroupWidget = new LayoutPartPartGroupWidget();
		$testingWidget = $layoutPartPartGroupWidget->getByPageId($tabLinks->id);

		$count=0;
		$headerAdditionalContent = '';

		while($count < 10)
		{
			if (isset($testingWidget[$count]['title']))
			{
				$headerAdditionalContent = $headerAdditionalContent.'var boxTitle'.($count+1).' = \''.ucwords($testingWidget[$count]['title']).'\';';
			}
			else 
			{
				$headerAdditionalContent = $headerAdditionalContent.'var boxTitle'.($count+1).' = \'Vacant widget area\';';
			}
			if (isset($testingWidget[$count]['title']))
			{
				$headerAdditionalContent = $headerAdditionalContent.'var box'.($count+1).' = \''.$testingWidget[$count]['source_code'].'\';';
			}
			else 
			{
				$headerAdditionalContent = $headerAdditionalContent.'var box'.($count+1).' = \'<p>Please insert a widget here</p>\';';
			}
			$count++;
		}
		#--------- end of widget display
		
		#------ background of top and below
		$wallpaper = new Wallpaper();
		$where = 'id = '.$tabLinks->top_wallpaper;
		$result = $wallpaper->fetchRow($where, 'id');
		$this->view->topWallpaper = $result->file; 
		$where = 'id = '.$tabLinks->body_wallpaper;
		$result = $wallpaper->fetchRow($where, 'id');
		$this->view->bodyWallpaper = $result->file; 
		#------ end of background of top and below
		
		
		#------ additional footer link
		$footerLinks2 = '';
		foreach ($addFooterLinks as $key)
		{
			$footerLinks2 = $footerLinks2.'<li><a href="'.$webInfo.'jumpsites/view/id/'.$jumpsiteId.'/goto/'.ucwords($key->id).'" title="'.$key->name.'"><span>'.ucwords($key->name).'</span></a></li>';
		}
		#---- additional footer link


		$this->view->headerAdditional = '
			<!-- GC -->
			<script type="text/javascript">
  				var footerLink = \'<div id="footerLink"><ul><li>Groupjump Beta version 2008</li>'.$footerLinks.'</ul>\'+\'<ul>'.$footerLinks2.'</ul></div>\';'.$headerAdditionalContent.'
			</script>
			<script type="text/javascript" src="'.$webInfo.'extjs/adapter/ext/ext-base.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/portal/ext-all.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/portal/Portal.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/portal/PortalColumn.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/portal/Ext.ux.MaximizeTool.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/portal/Portlet.js"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/portal/'.$jsLayout->js_file_for_layout.'"></script>
			<script type="text/javascript" src="'.$webInfo.'jscripts/portal/sample-grid.js"></script>
			<link rel="stylesheet" type="text/css" href="'.$webInfo.'css/portal/portal.css" />   
			<link rel="stylesheet" type="text/css" href="'.$webInfo.'css/portal/sample.css" />
			<!-- ENDLIBS -->
			';
	}
}